﻿using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Slwy.JwtAuth
{
    public class ParamConvert
    {
        /// <summary>
        /// 根据参数返回字典对象
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public static Dictionary<string, object> GetParamDic(string param)
        {
            Dictionary<string, object> dic = new Dictionary<string, object>();
            string[] paramArr = param.Split('&');
            for (int i = 0; i < paramArr.Length; i++)
            {
                var item = paramArr[i];
                string[] value = item.Split('=');
                dic.Add(value[0], value[1]);
            }

            return dic;
        }

        /// <summary>
        /// Jsons to dic.
        /// </summary>
        /// <param name="jsonParam">The json parameter.</param>
        /// <returns></returns>
        /// <exception cref="FormatException">ProductDetails不符合json格式.</exception>
        public static Dictionary<string, object> JsonToDic(string jsonParam)
        {
            Dictionary<string, object> dic = new Dictionary<string, object>();

            if (!string.IsNullOrWhiteSpace(jsonParam))
            {
                try
                {
                    var obj = JToken.Parse(jsonParam);
                    dic = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonParam);
                }
                catch (Exception)
                {
                    dic = ParamConvert.GetParamDic(jsonParam);
                }

            }

            if (!dic.ContainsKey("Timestamp"))
            {
                dic.Add("Timestamp", DateTime.Now.ToString("yyyyMMddHHmmss"));
            }



            return dic;
        }

        /// <summary>
        /// 除去数组中的空值和签名参数并以字母a到z的顺序排序
        /// </summary>
        /// <param name="dicArrayPre">过滤前的参数组</param>
        /// <returns>过滤后的参数组</returns>
        public static SortedDictionary<string, object> FilterPara(Dictionary<string, object> dicArrayPre)
        {
            SortedDictionary<string, object> dicArray = new SortedDictionary<string, object>();
            foreach (KeyValuePair<string, object> temp in dicArrayPre)
            {
                if (temp.Key.ToLower() != "sign_type" && temp.Value != null)
                {
                    dicArray.Add(temp.Key.ToLower(), temp.Value);
                }
            }

            return dicArray;
        }

        /// <summary>
        /// 把数组所有元素，按照“参数=参数值”的模式用“&”字符拼接成字符串
        /// </summary>
        /// <param name="dicArray">需要拼接的数组</param>
        /// <returns>拼接完成以后的字符串</returns>
        public static string CreateLinkString(Dictionary<string, object> dicArray)
        {
            StringBuilder prestr = new StringBuilder();
            foreach (KeyValuePair<string, object> temp in dicArray)
            {
                prestr.Append(temp.Key + "=" + temp.Value + "&");
            }

            ////去掉最後一個&字符
            int nlen = prestr.Length;
            prestr.Remove(nlen - 1, 1);
            return prestr.ToString();
        }
    }
}
